angular.module('app.baseinfo')
    .controller('logisticsCtrl', ['$scope', '$filter', '$modal', 'toaster', 'logisticsService',
        function ($scope, $filter, $modal, toaster, logisticsService) {
            $scope.query = "";
            $scope.pager = {
                pageNo: 1,
                pageSize: 10,
                totalItems: 0
            };
            $scope.getLogistics = function () {
                var query = {
                    pageNo: $scope.pager.pageNo,
                    pageSize: $scope.pager.pageSize
                };
                if (query&&$scope.query) {
                    query.query = $scope.query;
                }
                logisticsService.getLogistics(query).then(function (res) {
                    $scope.pager.totalItems = res.data.totalItems;
                    $scope.logistics = res.data.data;
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "查询失败")
                });
            };
            $scope.getLogistics();

            $scope.searchLogistics = function () {
                $scope.pager = {
                    pageNo: 1,
                    pageSize: 10,
                    totalItems: 0
                };
                $scope.getLogistics();
            };

            $scope.addLogistics=function(){
                var modalInstance = $modal.open({
                    animation: true,
                    templateUrl: 'partials/baseinfo/add_logistics-modal.html',
                    controller: 'addLogisticsCtrl',
                    size: 'md',
                    resolve: {
                        logistics: function () {
                            return null;
                        }
                    }
                });
                modalInstance.result.then(function () {
                    $scope.getLogistics();
                });
            };

            $scope.updateLogistics=function(logistics){
                var modalInstance = $modal.open({
                    animation: true,
                    templateUrl: 'partials/baseinfo/update_logistics-modal.html',
                    controller: 'updateLogisticsCtrl',
                    size: 'md',
                    resolve: {
                        logistics: function () {
                            return logistics;
                        }
                    }
                });
                modalInstance.result.then(function () {
                    $scope.getLogistics();
                });
            };

            $scope.deleteLogistics=function(logistics){
                var modalInstance = $modal.open({
                    animation: true,
                    templateUrl: 'partials/confirm/confirm.html',
                    controller: 'confirmCtrl',
                    size: 'sm',
                    resolve: {
                        content: function () {
                            return "确认删除？";
                        }
                    }
                });
                modalInstance.result.then(function () {
                    logisticsService.deleteLogistics(logistics).then(function (res) {
                        toaster.pop("success", "成功", "删除成功");
                        $scope.getLogistics();
                    }, function (res) {
                        toaster.pop("error", "错误", res.errMsg || "删除失败")
                    });
                });
            };
        }])
    .controller('addLogisticsCtrl', ['$scope', '$filter', '$modal', '$modalInstance', 'toaster', 'logisticsService',
        function ($scope, $filter, $modal, $modalInstance, toaster, logisticsService) {
            $scope.logistics = {};
            $scope.addLogistics = function () {
                logisticsService.addLogistics($scope.logistics).then(function (res) {
                    toaster.pop("success", "成功", "添加运输公司成功");
                    $modalInstance.close();
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "添加运输公司失败");
                });
            };
            $scope.cancel = function () {
                $modalInstance.dismiss('cancel');
            };
        }])
    .controller('updateLogisticsCtrl', ['$scope', '$filter', '$modal', '$modalInstance', 'toaster', 'logisticsService', 'logistics',
        function ($scope, $filter, $modal, $modalInstance, toaster, logisticsService, logistics) {
            $scope.logistics={};
            $scope.logistics = logistics;
            $scope.updateLogistics = function () {
                logisticsService.updateLogistics($scope.logistics).then(function (res) {
                    toaster.pop("success", "成功", "更新用户成功");
                    $modalInstance.close();
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "更新用户失败");
                });
            };
            $scope.cancel = function () {
                $modalInstance.dismiss('cancel');
            };
        }]);